#ifndef _BOUNDARY_H_
#define _BOUNDARY_H_

#define KEY_BOUNDARY_ROOT_NAME_POSITION "listpos"
#define KEY_BOUNDARY_POS_X	"x"
#define KEY_BOUNDARY_POS_Y	"y"

enum BoundaryObjPosition
{
	BoundaryObjPositionTopLeft = 0,
	BoundaryObjPositionTopRight,
	BoundaryObjPositionMidLeft,
	BoundaryObjPositionMidRight,
	BoundaryObjPositionBotLeft,
	BoundaryObjPositionBotRight,
	BoundaryObjPositionAnchor,
	BoundaryObjPositionCenter,
	BoundaryObjPositionCountPosition
};

class BoundaryObj
{
private:
	char* nameBoundary(BoundaryObjPosition pos)
	{
		switch(pos)
		{
			case BoundaryObjPositionTopLeft: return "topleft";
			case BoundaryObjPositionTopRight: return "topright";
			case BoundaryObjPositionMidLeft: return "midleft";
			case BoundaryObjPositionMidRight: return "midright";
			case BoundaryObjPositionBotLeft: return "botleft";
			case BoundaryObjPositionBotRight: return "botright";
			case BoundaryObjPositionCenter: return "center";
			case BoundaryObjPositionAnchor: return "anchor";	
		}
	}

	static BoundaryObj* m_root;

	string m_name;
	vector<BoundaryObj*> m_children;
	map<BoundaryObjPosition, CCPoint> m_value;

	BoundaryObj(string name)
	{
		this->m_name = name;
	};

	
	bool readXMLFile()
	{
		BoundaryObj* node = this;
		do
		{
			tinyxml2::XMLElement* curNode = NULL;
			tinyxml2::XMLElement* rootNode = NULL;
			
			tinyxml2::XMLDocument* xmlDoc = new tinyxml2::XMLDocument();
			unsigned long nSize;
			const char* pXmlBuffer = (const char*)CCFileUtils::sharedFileUtils()->getFileData((string("images/") + XML_BOUNDARY_FILE_NAME).c_str(), "rb", &nSize);

			if(NULL == pXmlBuffer)
			{
				CCLOG("can not read xml file");
				break;
			}

			xmlDoc->Parse(pXmlBuffer, nSize);
			delete[] pXmlBuffer;

			// get root node here
			rootNode = xmlDoc->RootElement();
			if (!rootNode)
			{
				CCLOG("read root node error");
				break;
			}
			
			curNode = rootNode->FirstChildElement();//getRoot
		
			while(curNode)
			{								
				if(curNode->FirstChildElement("name"))
				{
					
					CCLOG("%s", curNode->FirstChildElement()->GetText());
					node->addChild(curNode->FirstChildElement("name")->GetText()); //get name Node					
					string st = curNode->FirstChildElement("name")->GetText();	//assign name node		
					this->m_root->m_name = st;
					this->m_root->addChild(st);			


					for(int i = 0; i<BoundaryObjPositionCountPosition; i++)
					{
						if(curNode->FirstChildElement(KEY_BOUNDARY_ROOT_NAME_POSITION) && curNode->FirstChildElement(KEY_BOUNDARY_ROOT_NAME_POSITION)->FirstChildElement(nameBoundary(BoundaryObjPosition(i))) )
						{
							node->getChild(st)->setValue(BoundaryObjPosition(i), ccp(	(float)atof(curNode->FirstChildElement(KEY_BOUNDARY_ROOT_NAME_POSITION)->FirstChildElement(nameBoundary(BoundaryObjPosition(i)))->FirstChildElement(KEY_BOUNDARY_POS_X)->GetText()),
														   								(float)atof(curNode->FirstChildElement(KEY_BOUNDARY_ROOT_NAME_POSITION)->FirstChildElement(nameBoundary(BoundaryObjPosition(i)))->FirstChildElement(KEY_BOUNDARY_POS_Y)->GetText())));							
							this->m_root->getChild(st)->setValue(BoundaryObjPosition(i),node->getChild(st)->getValue(BoundaryObjPosition(i)));
						}
						else if(curNode->FirstChildElement(nameBoundary(BoundaryObjPosition(i))))
						{
							node->getChild(st)->setValue(BoundaryObjPosition(i), ccp(	(float)atof(curNode->FirstChildElement(nameBoundary(BoundaryObjPosition(i)))->FirstChildElement(KEY_BOUNDARY_POS_X)->GetText()),
														   								(float)atof(curNode->FirstChildElement(nameBoundary(BoundaryObjPosition(i)))->FirstChildElement(KEY_BOUNDARY_POS_Y)->GetText())));							
							this->m_root->getChild(st)->setValue(BoundaryObjPosition(i),node->getChild(st)->getValue(BoundaryObjPosition(i)));
						}
						else
						{						
							continue;
						}
					}
				}
				curNode = curNode->NextSiblingElement();
			}
			delete xmlDoc;
			return true;
		} while(0);
		return false;
	}
public:

	static BoundaryObj* getRoot()
	{
		if (m_root == NULL)
		{
			m_root = new BoundaryObj("root");
			m_root->readXMLFile();
		}
		return m_root;
	}

	void addChild(string name)
	{
		m_children.push_back(new BoundaryObj(name));
	}

	BoundaryObj* getChild(string childName)
	{
		for (unsigned int i=0; i < m_children.size(); i++)
		{
			if (m_children[i]->m_name == childName)
			{
				return m_children.at(i);
			}
		}
		addChild(childName);
		return getChild(childName);
	}

	void setValue(BoundaryObjPosition pos, CCPoint value)
	{
		m_value[pos] = value;
	}

	CCPoint getValue(BoundaryObjPosition pos)
	{
		return m_value[pos];
	}
};

#endif